/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package system.database.mysql5;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;

import com.aionemu.commons.database.DatabaseFactory;
import com.aionemu.gameserver.dao.PlayerBindPointDAO;
import com.aionemu.gameserver.model.gameobjects.player.BindPointPosition;
import com.aionemu.gameserver.model.gameobjects.player.Player;

/**
 * @author Mr. Poke
 *
 */
public class MySQL5PlayerBindPointDAO extends PlayerBindPointDAO
{

	/** Logger */
	private static final Logger	log				= Logger.getLogger(MySQL5PlayerBindPointDAO.class);

	public static final String	INSERT_QUERY	= "INSERT INTO `player_bind_point` (`player_id`, `map_id`, `x`, `y`, `z`, `heading`) VALUES (?,?,?,?,?,?)";
	public static final String	DELETE_QUERY	= "DELETE FROM `player_bind_point` WHERE `player_id`=?";
	public static final String	SELECT_QUERY	= "SELECT `map_id`, `x`, `y`, `z`, `heading` FROM `player_bind_point` WHERE `player_id`=?";
	public static final String	UPDATE_QUERY	= "UPDATE player_bind_point set `map_id`=?, `x`=?, `y`=? , `z`=?, `heading`=? WHERE `player_id`=?";
	
	/* (non-Javadoc)
	 * @see com.aionemu.commons.database.dao.DAO#supports(java.lang.String, int, int)
	 */
	@Override
	public boolean supports(String databaseName, int majorVersion, int minorVersion)
	{
		return MySQL5DAOUtils.supports(databaseName, majorVersion, minorVersion);
	}

	/* (non-Javadoc)
	 * @see com.aionemu.gameserver.dao.PlayerBindPointDAO#loadBindPointStat(com.aionemu.gameserver.model.gameobjects.player.Player)
	 */
	@Override
	public void loadBindPoint(Player player)
	{

		Connection con = null;
		try
		{
			con = DatabaseFactory.getConnection();
			PreparedStatement stmt = con.prepareStatement(SELECT_QUERY);
			stmt.setInt(1, player.getObjectId());
			ResultSet rset = stmt.executeQuery();
			if (rset.next())
			{
				int mapId = rset.getInt("map_id");
				float x = rset.getFloat("x");
				float y = rset.getFloat("y");
				float z = rset.getFloat("z");
				byte heading = rset.getByte("heading");
				player.setBindPoint(new BindPointPosition(mapId, x, y, z, heading));
			}
			rset.close();
			stmt.close();
		}
		catch (Exception e)
		{
			log.fatal("Could not restore BindPointPosition data for playerObjId: " + player.getObjectId() + " from DB: " + e.getMessage(), e);
		}
		finally
		{
			DatabaseFactory.close(con);
		}
	}

	/* (non-Javadoc)
	 * @see com.aionemu.gameserver.dao.PlayerBindPointDAO#insertBindPointStat(com.aionemu.gameserver.model.gameobjects.player.Player)
	 */
	@Override
	public void insertBindPoint(Player player)
	{
		if (player.getBindPoint() == null)
			return;
		Connection con = null;
		try
		{
			con = DatabaseFactory.getConnection();
			PreparedStatement stmt = con.prepareStatement(INSERT_QUERY);
			BindPointPosition bpp = player.getBindPoint();
			stmt.setInt(1, player.getObjectId());
			stmt.setInt(2, bpp.getMapId());
			stmt.setFloat(3, bpp.getX());
			stmt.setFloat(4, bpp.getY());
			stmt.setFloat(5, bpp.getZ());
			stmt.setByte(6, bpp.getHeading());
			stmt.execute();
			stmt.close();
		}
		catch (Exception e)
		{
			log.fatal("Could not store BindPointPosition data for player " + player.getObjectId() + " from DB: " + e.getMessage(), e);
		}
		finally
		{
			DatabaseFactory.close(con);
		}
	}

	/* (non-Javadoc)
	 * @see com.aionemu.gameserver.dao.PlayerBindPointDAO#updateBindPointStat(com.aionemu.gameserver.model.gameobjects.player.Player)
	 */
	@Override
	public void updateBindPoint(Player player)
	{
		if (player.getBindPoint() == null)
			return;
		Connection con = null;
		try
		{
			con = DatabaseFactory.getConnection();
			PreparedStatement stmt = con.prepareStatement(UPDATE_QUERY);
			BindPointPosition bpp = player.getBindPoint();
			stmt.setInt(1, bpp.getMapId());
			stmt.setFloat(2, bpp.getX());
			stmt.setFloat(3, bpp.getY());
			stmt.setFloat(4, bpp.getZ());
			stmt.setByte(5, bpp.getHeading());
			stmt.setFloat(6, player.getObjectId());
			stmt.execute();
			stmt.close();
		}
		catch (Exception e)
		{
			log.fatal("Could not update BindPointPosition data for player " + player.getObjectId() + " from DB: " + e.getMessage(), e);
		}
		finally
		{
			DatabaseFactory.close(con);
		}
	}

	/* (non-Javadoc)
	 * @see com.aionemu.gameserver.dao.PlayerBindPointDAO#deleteBindPointStat(int)
	 */
	@Override
	public void deleteBindPoint(int playerId)
	{

		Connection con = null;
		try
		{
			con = DatabaseFactory.getConnection();
			PreparedStatement stmt = con.prepareStatement(DELETE_QUERY);
			stmt.setInt(1, playerId);
			stmt.execute();
			stmt.close();
		}
		catch (Exception e)
		{
			log.fatal("Could not delete BindPointPosition data for player " + playerId + " from DB: " + e.getMessage(), e);
		}
		finally
		{
			DatabaseFactory.close(con);
		}
	
	}

}
